// pages/ser-detail-add/ser-detail-add.js
var app = getApp();
var main = require("../../../main.js");
var WxParse = require('../../../wxParse/wxParse.js');
const config = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    title:'系统阻力计算',
    id:'',
    del_show_flg:0,
    s_name1: '',
    s_name2: '',
    s_number: '',
    num1: '',
    num2: '',
    num3: '',
    num4: '',
    num5: '',
    num55: '',
    num6: '',
    num66: '',
    num7: '',
    num77: '',
    r_num1: '',
    r_num11: '',
    r_num2: '',
    r_num22: '',
    r_num3: '',
    r_num33: '',
    r_num4: '',
    r_num44: '',
    r_num5: '',
    r_num55: '',
    r_num1010: '',
    news:'' 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.checkUserAuthorization();
    this.getnews();
    this.setData({
      id: options.id,
      del_show_flg: options.del_show_flg,
      s_number: options.s_number,
      num1: options.num1,
      num2: options.num2,
      num3: options.num3,
      num4: options.num4,
      s_name1: options.s_name1,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 浏览大图
  onClick: function(e) {
    console.log(e, 'e')
    wx.previewImage({
      urls: ['https://img.yzcdn.cn/vant/cat.jpeg']
    })
  },

  goBack: function() {
    wx.switchTab({
      url: '/pages/ser-list/ser-list'
    });
  },

  goAdd:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    if(that.data.r_num1 === '' && that.data.r_num2 === '' && that.data.r_num3 === '' && that.data.r_num4 === '' && that.data.r_num5 === '' && that.data.r_num11 === '' && that.data.r_num22 === '' && that.data.r_num33 === '' && that.data.r_num44 === '' && that.data.r_num55 === ''){
      wx.hideLoading();
      wx.showToast({
        title: '当前页面没有计算结果，无法添加',
        icon: 'none',
        duration: 3000
      })
    }else{
      wx.request({
        url: app.domain_path + app.api_path + '/smoke_ventilation_insert',
        method: 'post',
        data: {
          token: main.get_storage('token'),
          smoke_type:43,
          s_name1: that.data.s_name1,
          s_name2: that.data.s_name2,
          s_number: that.data.s_number,
          num1: that.data.num1,
          num2: that.data.num2,
          num3: that.data.num3,
          num4: that.data.num4,
          num5: that.data.num5,
          num6: that.data.num6,
          num7: that.data.num7,
          num55: that.data.num55,
          num66: that.data.num66,
          num77: that.data.num77,
          r_num1: that.data.r_num1,
          r_num2: that.data.r_num2,
          r_num3: that.data.r_num3,
          r_num4: that.data.r_num4,
          r_num5: that.data.r_num5,
          r_num11: that.data.r_num11,
          r_num22: that.data.r_num22,
          r_num33: that.data.r_num33,
          r_num44: that.data.r_num44,
          r_num55: that.data.r_num55
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (!res.data) {
            wx.showToast({
              title: '加载错误',
              icon: 'loading',
              duration: 3000
            })
          }
          if (res.data.RESULT == 'OK') {
            wx.hideLoading();
            console.log(res.data.data);
            wx.navigateTo({
              url: "/pages/xitong/ser-detail-add2/ser-detail-add?del_show_flg=1&id=" + that.data.id + "&s_number=" + res.data.data 
              + "&s_name1=" + that.data.s_name1 + "&num1=" + that.data.num1 + "&num2=" + that.data.num2 + "&num3=" + that.data.num3 + "&num4=" + that.data.num4
            });
          } else {
            wx.showToast({
              title: res.data.MESSAGE,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    }
  },

  goDelete:function(){
    wx.navigateBack({
      delta: 1,
      success: function() {
        wx.navigateTo({
          url: '/pages/currentPage/currentPage',
        });
      }
    });
  },

  goSave:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    
    if(that.data.r_num1 === '' && that.data.r_num2 === '' && that.data.r_num3 === '' && that.data.r_num4 === '' && that.data.r_num5 === '' && that.data.r_num11 === '' && that.data.r_num22 === '' && that.data.r_num33 === '' && that.data.r_num44 === '' && that.data.r_num55 === ''){
      wx.hideLoading();
      wx.showToast({
        title: '没有计算结果，无法保存。',
        icon: 'none',
        duration: 3000
      })
    }else{
      wx.request({
        url: app.domain_path + app.api_path + '/smoke_ventilation_insert',
        method: 'post',
        data: {
          token: main.get_storage('token'),
          smoke_type:43,
          s_name1: that.data.s_name1,
          s_name2: that.data.s_name2,
          s_number: that.data.s_number,
          num1: that.data.num1,
          num2: that.data.num2,
          num3: that.data.num3,
          num4: that.data.num4,
          num5: that.data.num5,
          num6: that.data.num6,
          num7: that.data.num7,
          num55: that.data.num55,
          num66: that.data.num66,
          num77: that.data.num77,
          r_num1: that.data.r_num1,
          r_num2: that.data.r_num2,
          r_num3: that.data.r_num3,
          r_num4: that.data.r_num4,
          r_num5: that.data.r_num5,
          r_num11: that.data.r_num11,
          r_num22: that.data.r_num22,
          r_num33: that.data.r_num33,
          r_num44: that.data.r_num44,
          r_num55: that.data.r_num55,
          r_num1010:that.data.r_num1010,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (!res.data) {
            wx.showToast({
              title: '加载错误',
              icon: 'loading',
              duration: 3000
            })
          }
          if (res.data.RESULT == 'OK') {
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 3000
            })
            that.setData({
              s_number: res.data.data,
            })
            wx.switchTab({  
              url: "/pages/search-list/search-list"
            })
          } else {
            wx.showToast({
              title: res.data.MESSAGE,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    }
  },

  goReset:function(){
    this.setData({
      s_name1: '',
      s_name2: '',
      num1: '',
      num2: '',
      num3: '',
      num4: '',
      num5: '',
      num6: '',
      num7: '',
      num55: '',
      num66: '',
      num77: '',
      r_num1: '',
      r_num2: '',
      r_num3: '',
      r_num4: '',
      r_num5: '',
      r_num11: '',
      r_num22: '',
      r_num33: '',
      r_num44: '',
      r_num55: ''
    });
    wx.showToast({
      title: "重置成功",
      icon: 'none',
      duration: 3000
    })
  },

  goSmokeVentilation:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    // 正则表达式，匹配整数或小数
    const reg = /^[0-9]*\.?[0-9]*$/;
    // 如果输入值符合正则表达式，则更新数据
    if (!reg.test(that.data.num1)
      || !reg.test(that.data.num2)
      || !reg.test(that.data.num3)
      || !reg.test(that.data.num4)
      || !reg.test(that.data.num5)
      || !reg.test(that.data.num6)
      || !reg.test(that.data.num7)
      || !reg.test(that.data.num55)
      || !reg.test(that.data.num66)
      || !reg.test(that.data.num77)) {
        wx.showToast({
          title: "请检查输入有效的整数或者小数",
          icon: 'none'
        });
        return;
    }
    wx.request({
      url: app.domain_path + app.api_path + '/smoke_ventilation',
      method: 'post',
      data: {
        token: main.get_storage('token'),
        smoke_type:43,
        s_number: that.data.s_number,
        num1: that.data.num1,
        num2: that.data.num2,
        num3: that.data.num3,
        num4: that.data.num4,
        num5: that.data.num5,
        num6: that.data.num6,
        num7: that.data.num7,
        num55: that.data.num55,
        num66: that.data.num66,
        num77: that.data.num77
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (!res.data) {
          wx.showToast({
            title: '加载错误',
            icon: 'loading',
            duration: 3000
          })
        }
        if (res.data.RESULT == 'OK') {
          wx.hideLoading();
          wx.showToast({
            title: '计算成功',
            icon: 'none',
            duration: 3000
          })
          that.setData({
            r_num1: res.data.data.smoke_ventilation_info.r_num1,
            r_num2: res.data.data.smoke_ventilation_info.r_num2,
            r_num3: res.data.data.smoke_ventilation_info.r_num3,
            r_num4: res.data.data.smoke_ventilation_info.r_num4,
            r_num5: res.data.data.smoke_ventilation_info.r_num5,
            r_num11: res.data.data.smoke_ventilation_info.r_num11,
            r_num22: res.data.data.smoke_ventilation_info.r_num22,
            r_num33: res.data.data.smoke_ventilation_info.r_num33,
            r_num44: res.data.data.smoke_ventilation_info.r_num44,
            r_num55: res.data.data.smoke_ventilation_info.r_num55
          })
        } else {
          wx.showToast({
            title: res.data.MESSAGE,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  },

  checkUserAuthorization: function () {
    const that = this;
    // 获取用户的授权信息
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          console.log(main.get_storage('token') != '');
          if(main.get_storage('token') != []){
          }else{
            this.jumpConfirm();
          }
        } else {
          this.jumpConfirm();
        }
      },
      fail: (err) => {
        wx.showToast({
          title: err,
          icon: 'none'
        });
      }
    });
  },
  jumpConfirm:function(){
    wx.showModal({
      title: '温馨提示',
      content: '此功能需要授权登录，请确认~',
      success: (res) => {
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/index/index'
          });
        } else if (res.cancel) {
          wx.switchTab({
            url: '/pages/ser-list/ser-list',
          });
        }
      },
    });
  },
  handleInput: function(e) {  
    // 当输入框内容变化时，更新data中的inputValue  
    this.setData({  
      r_num1010: e.detail.value  
    });  
  }, 
  getnews:function(){
    var that = this;
    wx.request({
      url: app.domain_path + app.api_path + '/get_column_info',
      method: 'post',
      data: {
        token: main.get_storage('token'),
        id:43,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (!res.data) {
          wx.showToast({
            title: '加载错误',
            icon: 'loading',
            duration: 3000
          })
        }
        if (res.data.RESULT == 'OK') {
          wx.hideLoading();
          that.setData({
            news: res.data.data
          })
          //商品描述富文本编辑器
          WxParse.wxParse('article', 'html', that.data.news, that, 5);
        } else {
          wx.showToast({
            title: res.data.MESSAGE,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  }, 
})